package com.club.chelseaclub.tab;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.club.chelseaclub.R;
import com.club.chelseaclub.util.SinaNewsRestClient;
import com.loopj.android.http.AsyncHttpResponseHandler;

public class Tab3Activity extends Activity {
	
	private ProgressBar loading_progressBar;
	private List<Map<String, Object>> arrageList = new ArrayList<Map<String, Object>>();
	ListView list;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.qex_arrange);
		
		list = (ListView) findViewById(R.id.ListView01);
		loading_progressBar = (ProgressBar) findViewById(R.id.loading_process_arrange);
		
		getArrageData();
		
	}
	
	private void getArrageData() {
		
		String url = "http://goal.sports.163.com/8/stat/standings/2012_3.html";
        
        SinaNewsRestClient.get(url, null, new AsyncHttpResponseHandler(){
			@Override
			public void onSuccess(String response) {
				Document doc = Jsoup.parse(response);
				Element newsContent = doc.select("div.leftList4").first();
				if(newsContent != null) {
					Elements es = doc.getElementsByTag("tr");
					if(es != null && es.size()>4) {
						for (int i=1; i<es.size()-2; i++) {
							Element e = es.get(i);
							Map<String, Object> map = new HashMap<String, Object>();
							String teamName = e.select("td").eq(1).text().toString();
							if(teamName.length()>5) {
								teamName = teamName.substring(0,4);
							}
				            map.put("no", e.select("td").eq(0).text().toString());
				            map.put("name", teamName);
				            map.put("game", e.select("td").eq(2).text().toString());
				            map.put("win", e.select("td").eq(3).text().toString());
				            map.put("ping", e.select("td").eq(4).text().toString());
				            map.put("lose", e.select("td").eq(5).text().toString());
				            map.put("goal", e.select("td").eq(6).text().toString());
				            map.put("shi", e.select("td").eq(7).text().toString());
				            map.put("js", e.select("td").eq(8).text().toString());
				            map.put("point", e.select("td").eq(10).text().toString());
				            arrageList.add(map);
						}
					}
					
					SimpleAdapter adapter = new SimpleAdapter(Tab3Activity.this,arrageList,R.layout.arrange_list,
				             new String[]{"no","name","game","win","ping","lose","goal","shi","js","point"},
				             new int[]{R.id.no,R.id.name,R.id.game,R.id.win,R.id.ping,R.id.lose,R.id.goal,R.id.shi,R.id.js,R.id.point});
					Tab3Activity.this.list.setAdapter(adapter);
					
					loading_progressBar.setVisibility(View.GONE);
				}
			}
			
			@Override
			public void onFailure(Throwable e, String response) {
				e.printStackTrace();
				//stopProgress(l, v, position);
				Log.d("===通信错误===", "通信错误response: " + response);
				Toast.makeText(getApplication(), "通信错误", Toast.LENGTH_SHORT).show();
			}
		});
        
        /*
        for(int i=0;i<20;i++)
        {
       	 Map<String, Object> map = new HashMap<String, Object>();
            map.put("no", i+1);
            map.put("name", "切尔西");
            map.put("game", "5");
            map.put("win", "4");
            map.put("ping", "0");
            map.put("lose", "1");
            map.put("goal", "10");
            map.put("shi", "2");
            map.put("js", "8");
            map.put("point", "15");
            list.add(map);
        }
        */
       // return arrageList;
    }
}
